package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.FeedsOutStoreService;
import com.suyuan.service.VaccOutStoreService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service(value = "feedsOutStoreService")
public class FeedsOutStoreServiceImpl implements FeedsOutStoreService {
    @Resource
    private FeedsMapper feedsMapper;

    @Resource
    private FeedsinMapper feedsinMapper;

    @Resource
    private FeedsoutMapper feedsoutMapper;

    public PageInfo inquiry(Integer feedsId, Date outDate, String remark, Page page) {
        FeedsoutExample feedsoutExample=new FeedsoutExample();
        FeedsoutExample.Criteria criteria=feedsoutExample.createCriteria();
        if(feedsId!=null){
            criteria.andFeedidEqualTo(feedsId);
        }
        if(outDate!=null){
            criteria.andOuttimeEqualTo(outDate);
        }
        if(remark!=null){
            criteria.andRemarkLike("%"+remark+"%");
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Feedsout> feedsouts=feedsoutMapper.selectByExample(feedsoutExample);
        PageInfo info = new PageInfo(feedsouts);
        return info;
    }

    /**
     * 饲料出库信息
     * @param feedsout 饲料出库信息
     * @param oldNumOutStore 原来的出库数量
     * @return
     */
    public int save(Feedsout feedsout,Float oldNumOutStore){
        int a=feedsoutMapper.insert(feedsout);
        if(a==0){
            return a;
        }
        int feedid=feedsout.getFeedid();
        //若插入饲料入出库信息表没出错，则修改饲料信息表的库存量
        Feeds feeds=feedsMapper.selectByPrimaryKey(feedsout.getFeedid());
        //库存量为原来的库存量-出库的数量
        feeds.setAmountStore(oldNumOutStore-feedsout.getOutquantity());
        feedsMapper.updateByPrimaryKeySelective(feeds);
        return 1;//成功
    }

    public int delete(Integer id){
        Feedsout feedsout=feedsoutMapper.selectByPrimaryKey(id);
        //删除时需要设置外键为空才能删除成功
        feedsout.setFeedid(null);
        int a=feedsoutMapper.updateByPrimaryKeySelective(feedsout);
        return feedsoutMapper.deleteByPrimaryKey(id);
    }


    public  Feedsout findByOutStoreId(Integer outStoreId){
        FeedsoutExample feedsoutExample=new FeedsoutExample();
        FeedsoutExample.Criteria criteria=feedsoutExample.createCriteria();
        if(outStoreId!=null){
            criteria.andOutstoreidEqualTo(outStoreId);
            return feedsoutMapper.selectByPrimaryKey(outStoreId);
        }
        return null;
    }

    /**
     * 修改饲料出库信息
     * @param feedsout 饲料出库信息
     * @param oldOutStoreNum 原来的出库数量
     * @return
     */
    public int update(Feedsout feedsout,Float oldOutStoreNum){
        //更新饲料出库信息之前首先要更新饲料种类信息的库存量
        //获取前后库存量的差值(差值可能为负数,通过"新的出库数量-原来的出库数量"得出)
        float between=feedsout.getOutquantity()-oldOutStoreNum;
        Feeds feeds=feedsMapper.selectByPrimaryKey(feedsout.getFeedid());
        //库存量为现在的库存量-差值
        feeds.setAmountStore(feeds.getAmountStore()-between);
        int code=feedsMapper.updateByPrimaryKeySelective(feeds);
        //如果修改饲料种类信息的库存量失败就返回不再继续执行
        if(code==0){
            return 0;
        }

        //最后更新修改后的饲料出库信息
        feedsoutMapper.updateByPrimaryKey(feedsout);
        return 1;
    }
}
